package com.hk.yajin.view;

import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hk.developmentlib.utills.UiUtil;
import com.hk.yajin.R;

import butterknife.BindView;

/**
 * 类描述：
 * 创建人：huj
 * 创建时间：2017/4/14 23:26
 */
public class DefaultView {

    @BindView(R.id.iv_default_lcon)
    ImageView ivDefaultLcon;
    @BindView(R.id.tv_hint)
    TextView tvHint;
    @BindView(R.id.tv_load)
    TextView tvLoad;
    @BindView(R.id.default_page_view)
    LinearLayout defaultPageView;

    DefaultViewBuild build = new DefaultViewBuild();

    private int tag = 0;
    private DefaultViewClickListener clickListener;

    public DefaultView() {
        init();
    }

    private void init() {
        defaultPageView = (LinearLayout) UiUtil.inflate(R.layout.default_view);
        tvLoad = (TextView) defaultPageView.findViewById(R.id.tv_load);
        tvHint = (TextView) defaultPageView.findViewById(R.id.tv_hint);
        ivDefaultLcon = (ImageView) defaultPageView.findViewById(R.id.iv_default_lcon);

        tvLoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener != null) {
                    clickListener.defaultViewClick(tag);
                }
            }
        });
    }

    public boolean isShow() {
        return defaultPageView.getVisibility() == View.VISIBLE;
    }

    public View getRootView() {
        return defaultPageView;
    }

    public DefaultViewBuild getBuild() {
        return build;
    }

    public void show() {
        if (defaultPageView.getVisibility() != View.VISIBLE) {
            reset();
            defaultPageView.setVisibility(View.VISIBLE);
        }
    }

    private void reset() {
        build.setIcon(R.mipmap.applogo)
                .setHintText(R.string.badnetwork)
                .setBtnText(R.string.again_load);
    }

    public void hide() {
        if (defaultPageView.getVisibility() != View.GONE) {
            defaultPageView.setVisibility(View.GONE);
        }
    }

    public void setClickListener(DefaultViewClickListener listener) {
        clickListener = listener;
    }

    public class DefaultViewBuild {

        public DefaultViewBuild setIcon(@DrawableRes int resId) {
            tag = resId;
            ivDefaultLcon.setImageResource(resId);
            return this;
        }

        public DefaultViewBuild setHintText(String text) {
            tvHint.setText(text);
            return this;
        }

        public DefaultViewBuild setHintText(@StringRes int resId) {
            tvHint.setText(resId);
            return this;
        }

        public DefaultViewBuild setBtnText(String text) {
            tvLoad.setText(text);
            return this;
        }

        public DefaultViewBuild setBtnVisible(int visibility) {
            tvLoad.setVisibility(visibility);
            return this;
        }

        public DefaultViewBuild setBtnText(@StringRes int resId) {
            tvLoad.setText(resId);
            return this;
        }

        public DefaultViewBuild setTag(int tagId) {
            DefaultView.this.tag = tagId;
            return this;
        }

        public void show() {
            if (defaultPageView.getVisibility() != View.VISIBLE) {
                if (TextUtils.isEmpty(tvLoad.getText().toString())) {
                    tvLoad.setVisibility(View.GONE);
                } else {
                    tvLoad.setVisibility(View.VISIBLE);
                }
                defaultPageView.setVisibility(View.VISIBLE);
            }
        }
    }

    public interface DefaultViewClickListener {
        void defaultViewClick(int tag);
    }
}
